﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

namespace TaxiDispatchClient
{
    public partial class TaxiRouteHistoryForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiRouteHistoryForm()
        {
            InitializeComponent();

            dateEdit1.DateTime = DateTime.Today.AddDays(-2);
            dateEdit2.DateTime = DateTime.Now;



            Thread tbind = new Thread(BindData);
            tbind.IsBackground = true;
            tbind.Start();
        }


        public TaxiRouteHistoryForm(DateTime start,DateTime end,string phoneNumber)
        {
            InitializeComponent();

            dateEdit1.DateTime = start;
            dateEdit2.DateTime = end;
            imageComboBoxEdit1.SelectedIndex = 1;
            btnText_phoneNumber.Text = phoneNumber;

            Thread tbind = new Thread(BindData);
            tbind.IsBackground = true;
            tbind.Start();
        }


        private DataTable _dtGroup;
        private List<string> _companylist;

        private void InitControl()
        {
            DataSet ds = null;
            do
            {
                ds = MainForm.PositionService.TaxiGroupGetList("");
                Thread.Sleep(500); 
            } while (ds==null || ds.Tables.Count==0);
            _dtGroup = ds.Tables[0];

            List<string> company;
            do
            {
                company = MainForm.PositionService.GetCompanyList();
            } while (company==null);
            _companylist = company;

        }

        private void BindData()
        {

            //string startTime="";
            //string endTime = "";
            //string phoneNumber = "";            
            int type = imageComboBoxEdit1.SelectedIndex; 
            try
            {
                //if (type == 0)
                //{
                //    startTime = dateEdit1.DateTime.ToString("yyyy-MM-dd") +
                //                " 00:00:00";
                //    endTime = dateEdit1.DateTime.ToString("yyyy-MM-dd") +
                //                " 23:59:59";
                //}
                //else if (type == 1)
                //{
                //    phoneNumber = btnText_phoneNumber.Text;
                //    startTime = dateEdit1.DateTime.ToString("yyyy-MM-dd") +
                //                " 00:00:00";
                //    endTime = dateEdit2.DateTime.ToString("yyyy-MM-dd") +
                //                " 23:59:59";
                //}
                //DataSet ds = MainForm.PositionService.TaxiRouteGetStatisticsByPara(
                //    phoneNumber, startTime, endTime);


                string phoneNumber = "";
                string startTime = (
                    dateEdit1.DateTime.ToString("yyyy-MM-dd") + " 00:00:00");
                string endTime = (
                    dateEdit2.DateTime.ToString("yyyy-MM-dd") + " 23:59:59");
                DataSet ds = null;
                if (type == 1)//单量
                {
                    phoneNumber = btnText_phoneNumber.Text;
                }
                else
                {
                    if (MainForm.MyUserInfo.CompanyName!=Service.PowerConfig.MainCompanyName)
                    {
                        endTime += "' and CompanyName='" + MainForm.MyUserInfo.CompanyName;
                    }                    
                }
                ds = MainForm.PositionService.TaxiRouteGetListByPhoneNumberAndTime(
                    phoneNumber, startTime, endTime);

                if (ds!=null && ds.Tables.Count>0)
                {
                     
                    BeginInvoke(new MethodInvoker(() =>
                        {
                            gridControl1.DataSource = ds.Tables[0];
                            gridView1.GroupPanelText = "统计结果";
                            btn_select.Enabled = true;
                        }));                        
                }                
            }
            catch (Exception ex)
            {                              
                btn_select.Enabled = true;
                if (!ex.Message.Contains("创建窗口句柄之前"))
                {
                    Common.CommonRoutine.HandlingExceptions(ex);
                }                
            }
        }



        private void imageComboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 0:                  
                    btnText_phoneNumber.Visible = false;
                    //label_to.Visible = false;
                    //dateEdit2.Visible = false;
                    //dateEdit1.DateTime = DateTime.Now;
                    break;
                case 1:
               
                    btnText_phoneNumber.Visible = true;
                    btnText_phoneNumber.Text = "";
                    //label_to.Visible = true;
                    //dateEdit2.Visible = true;
                    //dateEdit1.DateTime = DateTime.Now.AddDays(-10);
                    break;               
            }
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            btn_select.Enabled = false;
            if (!CheckPass())
            {                 
                btn_select.Enabled = true;                 
                return;
            }
            gridView1.GroupPanelText = "数据查询中...";
            Thread t = new Thread(BindData);
            t.IsBackground = true;
            t.Start();
        }


        private void btn_exportToExcel_MouseClick(object sender, MouseEventArgs e)
        {

            saveFileDialog1.FileName = "车辆行驶里程统计";
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private bool CheckPass()
        {
            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 1:
                    if (btnText_phoneNumber.Text.Trim() == "")
                    {
                        XtraMessageBox.Show("请输入要查询的车辆的手机号码！");
                        btnText_phoneNumber.Focus();
                        return false;
                    }
                    break;
             
            }
            return true;
        }


        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

        private void btnText_phoneNumber_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            DriverInfoForm form = new DriverInfoForm();
            form.ShowDialog();
            DataRow row = form.gridView1.GetDataRow(form.gridView1.FocusedRowHandle);
            if (row != null)
            {
                btnText_phoneNumber.Text = row["PhoneNumber"].ToString();                
            }
        }

       
        private void btn_report_Click(object sender, EventArgs e)
        {
            TaxiRouteStatisticsReportForm form = new TaxiRouteStatisticsReportForm();
            form.ShowDialog();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            TaxiRouteStatisticsListForm form = new TaxiRouteStatisticsListForm();
            form.ShowDialog();
        }
         
    }
}